package com.java.module.fire.controller.api;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.page.Page;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.WebKit;
import com.java.module.fire.domain.Friends;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.java.module.fire.service.IFriendsService;

import java.util.List;

/**
 * 用户好友Controller
 * 
 * @author Administrator
 * @date 2025-02-10
 */
@RestController("ApiFriendsController")
@RequestMapping("/fire/friends")
public class FriendsController {

    @Autowired
    IFriendsService friendsService;

    @Autowired
    ApiTokenService apiTokenService;



    /**
     * 新增用户好友
     */
    @Log(title = "用户好友", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Friends friends) {
        friends.setUid(apiTokenService.getLoginUserId());
        List<Friends> list = friendsService.selectFriendsList(friends);
        Friends friends1=new Friends();
        
        if(list.size()>0) {
            return AjaxResult.error("好友已存在");
        }
        friendsService.insertFriends(friends);

        friends1.setUid(friends.getToUid());
        friends1.setToUid(apiTokenService.getLoginUserId());

        friendsService.updateReadStatus(friends.getMid());


        return WebKit.toAjax(friendsService.insertFriends(friends1));
    }



    /**
     * 查询用户好友列表
     */
    @GetMapping("/list")
    public Page<Friends> list(Friends friends) {
        WebKit.startPage();
        friends.setUid(apiTokenService.getLoginUserId());
        List<Friends> list = friendsService.selectFriendsList(friends);
        return WebKit.getPageData(list);
    }





}